package com.shixulin.controller;


import com.shixulin.domain.ResponseResult;
import com.shixulin.entity.DTO.ExamPaperQueryDTO;
import com.shixulin.entity.ExamPaper;
import com.shixulin.entity.PaperQuestion;
import com.shixulin.service.ExamPaperService;
import com.shixulin.service.PaperQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher")
@CrossOrigin
public class ExamController {

    @Autowired
    private ExamPaperService examPaperService;
    @Autowired
    private PaperQuestionService paperQuestionService;
    //创建试卷
    @PostMapping("/createExamPaper")
    public ResponseResult createExamPaper(@RequestBody ExamPaper examPaper){
        return examPaperService.createExamPaper(examPaper);
    }

    //获取试卷信息
    @PostMapping("/getExamPaperList")
    public ResponseResult getExamPaperList(@RequestBody ExamPaperQueryDTO examPaperQueryDTO){
        return examPaperService.getExamPaperList(examPaperQueryDTO);
    }
    //发布考试
    @GetMapping("/publishPaper/{paperId}")
    public ResponseResult publishPaper(@PathVariable("paperId") int paperId){
        return examPaperService.publishPaper(paperId);
    }

    //取消发布
    @GetMapping("/cancelPublish/{paperId}")
    public ResponseResult cancelPublish(@PathVariable("paperId") int paperId){
        return examPaperService.cancelPublish(paperId);
    }

    //获取对应的试卷
    @GetMapping("/getExamPaperByPaperId/{paperId}")
    public ResponseResult getExamPaperByPaperId(@PathVariable("paperId") int paperId){
        return examPaperService.getExamPaperByPaperId(paperId);
    }
    //获取试卷题目
    @GetMapping("/getExamQuestionByPaperId/{paperId}")
    public ResponseResult getExamQuestionByPaperId(@PathVariable("paperId") int paperId){
        return examPaperService.getExamQuestionByPaperId(paperId);
    }

    //修改试卷
    @PostMapping("/editExamPaper")
    public ResponseResult editExamPaper(@RequestBody ExamPaper examPaper){
        return examPaperService.editExamPaper(examPaper);
    }
    //修改删除试卷后的总分
    @PostMapping("/updateExamScore")
    public ResponseResult updateExamScore(@RequestBody ExamPaper examPaper){
        return examPaperService.updateExamScore(examPaper);
    }
    //删除考试试卷
    @GetMapping("/deleteExamPaper/{paperId}")
    public ResponseResult deleteExamPaper(@PathVariable("paperId") int paperId){

        //先删除题目，再删除试卷
        boolean delete =  paperQuestionService.deletePaperQuestionById(paperId);
        if (delete) {
            return examPaperService.deleteExamPaper(paperId);
        }else {
            return new ResponseResult<>(505, "删除失败");
        }
    }


}
